# 企业微信第三方应用 Dockerfile
# WeChat Work Third-party Application Dockerfile

# 使用官方的 .NET 8.0 运行时作为基础镜像
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

# 设置环境变量
ENV ASPNETCORE_ENVIRONMENT=Production
ENV ASPNETCORE_URLS=http://+:80

# 使用 .NET 8.0 SDK 进行构建
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src

# 复制项目文件并还原依赖
COPY ["src/WeWorkApp.Api/WeWorkApp.Api.csproj", "src/WeWorkApp.Api/"]
RUN dotnet restore "src/WeWorkApp.Api/WeWorkApp.Api.csproj"

# 复制所有源代码
COPY . .

# 构建项目
WORKDIR "/src/src/WeWorkApp.Api"
RUN dotnet build "WeWorkApp.Api.csproj" -c Release -o /app/build

# 发布应用
FROM build AS publish
RUN dotnet publish "WeWorkApp.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false

# 最终镜像
FROM base AS final
WORKDIR /app

# 复制发布的文件
COPY --from=publish /app/publish .

# 复制前端文件到 wwwroot
COPY src/WeWorkApp.Web/ ./wwwroot/

# 创建非 root 用户
RUN adduser --disabled-password --gecos '' appuser && chown -R appuser /app
USER appuser

# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD curl -f http://localhost/health || exit 1

# 启动应用
ENTRYPOINT ["dotnet", "WeWorkApp.Api.dll"]